<template>
  <el-dialog :title="showTitle" :visible="showDialog" @close="closeDialog">
    <!-- 匿名插槽 -->
    <el-form ref="deptForm" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <div slot="footer">
      <el-button type="primary" size="small" @click="submit">确定</el-button>
      <el-button size="small" @click="closeDialog">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { reqGetEmployeeSimple } from '@/api/employees'
import { reqAddDepartments, reqGetDepartDetail, reqUpdateDepartments } from '@/api/departments'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    nodeData: {
      type: Object,
      required: true
    },
    depts: {
      type: Array,
      required: true
    }
  },
  data() {
    const validateName = (rule, value, callback) => {
      // console.log(value, this.nodeData)
      // 打印下一个部门的子部门 比如我们要拿财务部的子部门
      // 要拿一个部门的子部门，要传入这个部门的id
      let children = []
      if (this.form.id) {
        // 如果是编辑操作, 是可以和原来的自己的code重复的
        if (this.nodeData.name === value) {
          return callback()
        }
        children = this.depts.filter(item => item.pid === this.nodeData.pid)
      } else {
        children = this.depts.filter(item => item.pid === this.nodeData.id)
      }

      const isRepeat = children.some(item => item.name === value)
      isRepeat ? callback(new Error(`同级部门已有${value}部门名称`)) : callback()
    }
    const validateCode = (rule, value, callback) => {
      // value 是输入的内容
      if (this.form.id) {
        // 如果是编辑操作, 是可以和原来的自己的code重复的
        if (this.nodeData.code === value) {
          return callback()
        }
      }
      const isRepeat = this.depts.some(item => item.code === value)
      isRepeat ? callback(new Error(`已有${value}部门编码`)) : callback()
    }
    return {
      options: [],
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: validateName, trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: validateCode, trigger: ['blur', 'change'] }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  computed: {
    showTitle() {
      return this.form.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
    this.getEmployeeSimple()
  },
  methods: {
    async getDepartDetail(id) {
      const { data } = await reqGetDepartDetail(id)
      this.form = data
    },
    async submit() {
      try {
        await this.$refs.deptForm.validate()
        // console.log('ok')
        /**
         * 1. 封装新增保存接口并调用
         * 2. 提示成功信息
         * 1. 封装新增保存(编辑)接口并调用
         * 2. 提示成功(新增编辑)信息
         * 3. 刷新列表
         * 4. 关闭弹层
         */
        if (!this.form.id) {
          await reqAddDepartments({ ...this.form, pid: this.nodeData.id })
          // this.$message.success('新增部门成功')
        } else {
          await reqUpdateDepartments(this.form)
          // this.$message.success('修改部门成功')
        }
        this.$message.success(this.showTitle + '成功')
        this.$emit('add-depts')
        this.closeDialog()
      } catch (error) {
        console.log('catch')
      }
    },
    async getEmployeeSimple() {
      const { data } = await reqGetEmployeeSimple()
      // console.log(data)
      this.options = data
    },
    closeDialog() {
      // 重置数据
      this.form = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 管理者
        introduce: '' // 部门介绍
      }
      this.$refs.deptForm.resetFields()
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style>

</style>
